<template>
  <div>
    <div class="insect-tab">
      <div class="insect-tab-btn" @click="changeTabLeft">
        <i class="el-icon-caret-left"></i>
      </div>
        <div style="width: 308px;overflow: hidden;">
          <div class="insect-tab-box" :style="{left:activeTab > 2 ? -100 * (tabList.length - 2) + 'px' : ''}">
            <div class="insect-tab-item" @click="changeTab(item,index)" :class="{'active':activeTab == index}" v-for="(item,index) in tabList" :key="index">
              {{item.name}}
            </div>
          </div>
        </div>
      <div class="insect-tab-btn" @click="changeTabRight"><i class="el-icon-caret-right"></i></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'windowTab',
  data() {
    return {
      activeTab:0
    }
  },
  props:{
    tabList:{
      type:Array,
      default:()=>[
        // {title:'虫情监测 (三期)', active:false},
        // {title:'虫情监测 (三期)', active:true},
        // {title:'虫情监测 (三期)', active:false},
      ]
    },
    type:{
      type:String,
      default:''
    }
  },
  methods:{
    changeTabLeft() {
      if(this.activeTab == 0) return
      this.activeTab--
      this.$emit('changeTab',{index:this.activeTab,type:this.type})
    },
    changeTabRight() {
      if(this.activeTab == this.tabList.length - 1) return
      this.activeTab ++
      this.$emit('changeTab',{index:this.activeTab,type:this.type})
      console.log(this.activeTab);

    },
    changeTab(item,index){
      this.activeTab = index
      this.$emit('changeTab',{index:this.activeTab,type:this.type})
    }
  }
}
</script>

<style lang="scss" scoped>
$bgColor: rgba(31,104,105,0.2);
.insect-tab {
  margin-top: 16px;
  display: flex;
  align-items: center;
  .insect-tab-btn {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 10px;
    height: 24px;
    color: #00b23c;
    background: $bgColor;
    border-radius: 2px 2px 2px 2px;
  }
  .insect-tab-box {
    position: relative;
    // width: 308px;
    // overflow: hidden;
    margin: 0 2px;
    display: flex;
    align-items: center;
    // justify-content: space-around;
    font-size: 12px;
    color: rgba(255,255,255,.7);
    .insect-tab-item {
      flex-shrink: 0;//让子元素保持宽度
      cursor: pointer;
      width: 96px;
      height: 25px;
      line-height: 24px;
      // padding: 0 10px;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid rgba(255,255,255,.3);
      // display: flex;
      text-align: center;
      align-items: center;
      white-space: nowrap; /* 强制文本在一行内显示 */
      overflow: hidden; /* 隐藏溢出的文本 */
      text-overflow: ellipsis; /* 用省略号表示被修剪的文本 */
      margin-right:4px;

      // justify-content: center;
      &:nth-child(1) {
        margin-left: 0;

      }
      &:nth-last-child(1) {
        margin-right: 0;
      }
    }
    .active {
      // width: 96px;
      height: 24px;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid #ffffff;
      // display: flex;
      text-align: center;
      line-height: 23px;
      align-items: center;
      justify-content: center;
      background-color: #00b23c;
      white-space: nowrap; /* 强制文本在一行内显示 */
      overflow: hidden; /* 隐藏溢出的文本 */
      text-overflow: ellipsis; /* 用省略号表示被修剪的文本 */
      border: 1px solid #00b23c;
    }
  }
}
</style>
